/////////////////////////////////////////////////////////////////////////////////////////////		
*								 TABLE A.8 and TABLE A.9											*
/////////////////////////////////////////////////////////////////////////////////////////////	

use "${inputdir}/prospects_analysis_dataset_clean.dta", clear

// Preparing dataset

	drop if interviews!=2	
	drop working 
		
	foreach v in welfare_today selfesteem locuscontrol aggression riskybehavior{
		replace r1_`v' = r1_`v'_std
		replace r2_`v' = r2_`v'_std
		rename r1_`v' `v'1
		rename r2_`v' `v'2
		rename r1_`v'_std `v'_std1
		rename r2_`v'_std `v'_std2		
		gen `v'_L1 = `v'1
		}
		
		gen psi_L1 = psi_std1
		
	* Generate all PS variables and rename for reshape
		rename r1_loc* loc*X1
		rename r2_loc* loc*X2			
		rename r1_agr* agr*X1
		rename r2_agr* agr*X2	
		rename r1_sd_* sd_*X1
		rename r2_sd_* sd_*X2
		rename r1_LifeEvent* le*X1
		rename r2_LifeEvent* le*X2	
			
	keep female reg_age age2 r1_HighGrade le*X* sd*X* loc*X* agr*X*  lfi1anc Life* LEx* commid BOTH sfc_any *_L1  ///
		 psi* welfare_today* selfesteem* locuscontrol* aggression* riskybehavior* sfcgroupid ///
		 ANYSFC sfc_any respid female   

	reshape long working  loc1X loc2X loc3X loc4X loc5X loc6X loc7X loc8X loc9X loc10X loc11X loc12X loc13X loc14X loc15X loc16X   ///
		welfare_today selfesteem locuscontrol aggression riskybehavior ///
		agr1X agr2X agr3X agr4X agr5X agr6X agr7X welfare_today_std selfesteem_std locuscontrol_std aggression_std riskybehavior_std  ///
		sd_5X sd_6X sd_7X sd_8X sd_9X sd_10X psi psi_std LifeEvent_index  ///
		le1X le2X le3X le4X le5X le6X le7X le8X le9X le10X le11X le12X le13X le14X le15X le16X , i(respid) j(time)
	
	rename loc*X loc*
	rename agr*X agr*
	rename sd*X sd* 
	rename sd_* sd*
	rename le*X le*
	
	replace sfc_any = 0 if time==1
	gen SFC = (time==2) * ANYSFC
	
	replace LExSFC = 0 if time==1
	
	tempfile prepdata
	save `prepdata'	

local controls1 = "female reg_age age2 i.r1_HighGrade"
replace lfi1 = lfi1anc

local wf = "psi_std welfare_today"
local loc = "locuscontrol_std loc1 loc2 loc3 loc4 loc5 loc6 loc7 loc8"
local se = "selfesteem_std loc9 loc10 loc11 loc12 loc13 loc14 loc15 loc16"
local ag = "aggression_std agr1 agr2 agr3 agr4 agr5 agr6 agr7"
local rb = "riskybehavior_std sd5 sd6 sd7 sd8 sd9 sd10"
local le = "LifeEvent_index le1 le2 le3 le4 le5 le6 le7 le8 le9 le10 le11 le12 le13 le14 le15 le16"

matrix psresults_anc = [0 , 0 , 0, 0]

drop *_L1
gen Y_L1 = .

local t = 1
foreach oc in `wf' `loc' `se' `ag' `rb'{
		
		gen temp = `oc'
		replace temp = . if time==2
		bys respid: egen `oc'_L1 = max(temp)
		replace Y_L1 = `oc'_L1
		drop temp
		
		reghdfe `oc' SFC Y_L1 `controls1' if time==2 , absorb(commid) vce(cluster respid)
		matrix table = r(table)
		loc a1 = round(table[1,1], 0.0001)
		loc a2 = round(table[2,1], 0.0001)
		loc a3 = round(table[4,1], 0.0001)
		sum `oc' if ANYSFC==0 & time==2 		
		loc a4 = round(r(mean), 0.0001)
		
		matrix psresults_anc = [psresults_anc \ `a1', `a2', `a3', `a4']
	
		di "outcome `t'"
		local t = `t' + 1
		}
		
	matrix psresults_anc = psresults_anc[2..`t', .]	

	* APPENDIX TABLE A8
	
	matrix rownames psresults_anc = "Psychosocial Index" "Welfare today" "Locus of Control Index" "Your choices affect your future." ///
		"You have limited control over your life." "Business success is the result of luck." "Trying hard can improve your life." "When you make plans, you can usually make them work." ///
		"Bad things that happen to people are due to bad luck." "People who live on the streets are responsible for their situation." "Success comes from hard work." ///
		"Self-Esteem Index" "You are satisfied with yourself." "You feel useless sometimes" "Everything you try to do, you fail." "You do not have enough respect for yourself." ///
		"You feel that you are at least as good a person as most people." "You think that you are a good person but doing nothing." "You can do business as well as most others." ///
		"You feel ashamed of how your life is going." "Aggression Index" "...had disputes with leaders/elders." "...been involved in disputes with other community members." ///
		"...major disputes with a neighbor." "...major disputes with a family member." "...major disputes with a community leader." ///
		"...major disputes with police." "...been in a physical fight." ///
		"Risky Behavior Index" "...gambled or bet." "...smoked cigarettes." "...drank alcohol." "...smoked marijuana." "...used other heavy drugs." "...sold drugs."
		
	matrix colnames psresults_anc = SFC "Std Err" "P-Value" "Control Mean"	
	esttab matrix(psresults_anc) using "${outputdir}/tables/Table_A8.tex", replace title(Impact of SFC on All Psychosocial Sub-Indices and Underlying Questions) align(cccc)
		
	matrix finresults_ancy2 = [0 , 0 , 0]	
	local t = 1
	foreach oc in `le'{
	
			gen temp = `oc'
			replace temp = . if time==2
			bys respid: egen `oc'_L1 = max(temp)
			replace Y_L1 = `oc'_L1
			drop temp
			
			ttest `oc' if time==2 , by(ANYSFC)
			loc x3 = round(r(p), 0.0001)
			sum `oc' if ANYSFC==0 & time==2
			loc x4 = round(r(mean), 0.0001)
			loc x4b = round(r(sd), 0.0001)
			sum `oc' if ANYSFC==1 & time==2
			loc x5 = round(r(mean), 0.0001)		
			loc x5b = round(r(sd), 0.0001)			
			
			matrix finresults_ancy2 = [finresults_ancy2 \ `x5', `x4', `x3' \ `x5b', `x4b', .]	
		
			di "coping outcome `t'"
			local t = `t'+1
			}

	local k = ((`t' - 1) * 2) + 1
	matrix finresults_ancy2 = finresults_ancy2[2..`k', .]	
	
	* APPENDIX TABLE A9	
			
			matrix rownames finresults_ancy2 = "Life Event Index - Standardized" "\hspace{.2cm}" "Serious Illness *" "\hspace{.2cm}"  "Serious Accident *" "\hspace{.2cm}"  "Death of Friend/Family Member *" "\hspace{.2cm}"  "Divorce or Separation *" "\hspace{.2cm}"  ///
				"Lost Job" "\hspace{.2cm}"  "Not Able to Get a Job" "\hspace{.2cm}"  "Alcohol Related Problems" "\hspace{.2cm}"  ///
				"Drug Related Problems" "\hspace{.2cm}"  "Witness Violence" "\hspace{.2cm}"  "Abuse or Violent Crime" "\hspace{.2cm}"  ///
				"Trouble with Police" "\hspace{.2cm}"  "Gambling Problem" "\hspace{.2cm}"  "Family Member Sent to Jail *" "\hspace{.2cm}"  "Overcrowding at Home *" "\hspace{.2cm}"  ///
				"Discrimination/Racism *" "\hspace{.2cm}"  "Vandalism *" "\hspace{.2cm}" 

			matrix colnames finresults_ancy2 = "Treat Mean" "Control Mean" "P-Value of Diff"	
			esttab matrix(finresults_ancy2) using "${outputdir}/tables/Table_A9.tex", replace title(Life Events and SFC Treatment Status) align(ccc)			
